import type {
  ApplicationQuery,
  ApplicationResponse,
  CreateOrUpdatePositionRequest,
  PositionApplication,
  PositionFavorite,
  PositionQuery,
  PositionResponse,
  StudentPositionInfo
} from "./type"
import { request } from "@/http/axios"
import { getAvailableEnterpriseListApiMock } from "../../enterprise/apis/mock"
// 导入 mock 函数
import * as MockApis from "./mock"

// 判断是否使用 mock 数据（开发环境使用）
const USE_MOCK = import.meta.env.DEV

/** 获取岗位列表 */
export function getPositionListApi(params: PositionQuery) {
  if (USE_MOCK) {
    return MockApis.getPositionListApiMock(params)
  }
  return request<PositionResponse>({
    url: "internship/position",
    method: "get",
    params
  })
}

export function getAvailableEnterpriseListApi() {
  if (USE_MOCK) {
    return getAvailableEnterpriseListApiMock()
  }
  return request<ApiResponseData<Array<{ id: number, name: string }>>>({
    url: "internship/enterprise/available",
    method: "get"
  })
}

/** 创建岗位 */
export function createPositionApi(data: CreateOrUpdatePositionRequest) {
  if (USE_MOCK) {
    return MockApis.createPositionApiMock(data)
  }
  return request({
    url: "internship/position",
    method: "post",
    data
  })
}

/** 更新岗位 */
export function updatePositionApi(data: CreateOrUpdatePositionRequest) {
  if (USE_MOCK) {
    return MockApis.updatePositionApiMock(data)
  }
  return request({
    url: "internship/position",
    method: "put",
    data
  })
}

/** 更新岗位状态 */
export function updatePositionStatusApi(id: number, status: string) {
  if (USE_MOCK) {
    return MockApis.updatePositionStatusApiMock(id, status)
  }
  return request({
    url: `internship/position/${id}/status`,
    method: "put",
    data: { status }
  })
}

/** 上传岗位说明书 */
export function uploadJobDescriptionApi(file: File) {
  if (USE_MOCK) {
    return MockApis.uploadJobDescriptionApiMock(file)
  }
  const formData = new FormData()
  formData.append("file", file)
  return request<ApiResponseData<{ url: string }>>({
    url: "internship/position/upload-description",
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data"
    }
  })
}

/**
 * 获取可申请岗位列表（学生端）
 */
export async function getStudentPositionListApi(query: {
  currentPage: number
  size: number
  major?: string
  workType?: string[]
  workCity?: string
  positionName?: string
}) {
  if (USE_MOCK) {
    return MockApis.getStudentPositionListMock(query)
  }
  return request<ApiResponseData<{ list: StudentPositionInfo[], total: number }>>({
    url: "/internship/student/positions",
    method: "get",
    params: query
  })
}

/**
 * 获取岗位详情（学生端）
 */
export async function getStudentPositionDetailApi(id: number) {
  if (USE_MOCK) {
    return MockApis.getStudentPositionDetailMock(id)
  }
  return request<ApiResponseData<StudentPositionInfo>>({
    url: `/internship/student/position/${id}`,
    method: "get"
  })
}

/**
 * 创建岗位申请
 */
export async function createPositionApplicationApi(data: Omit<PositionApplication, "id" | "status" | "applyTime" | "positionName">) {
  if (USE_MOCK) {
    return MockApis.createPositionApplicationMock(data)
  }
  return request<ApiResponseData<PositionApplication>>({
    url: "/internship/position/application",
    method: "post",
    data
  })
}

/**
 * 获取岗位申请列表（老师审核用）
 */
export async function getPositionApplicationsApi(query: ApplicationQuery) {
  if (USE_MOCK) {
    return MockApis.getPositionApplicationsMock(query)
  }
  return request<ApplicationResponse>({
    url: "/internship/position/applications",
    method: "get",
    params: query
  })
}

/**
 * 审核岗位申请
 */
export async function reviewPositionApplicationApi(id: number, data: { status: "approved" | "rejected", comment?: string }) {
  if (USE_MOCK) {
    return MockApis.reviewPositionApplicationMock(id, data)
  }
  return request<ApiResponseData<PositionApplication>>({
    url: `/internship/position/application/${id}/review`,
    method: "patch",
    data
  })
}

/**
 * 收藏/取消收藏岗位
 */
export async function toggleFavoriteApi(positionId: number, isFavorite: boolean) {
  if (USE_MOCK) {
    return MockApis.toggleFavoriteMock(positionId, isFavorite)
  }
  if (isFavorite) {
    return request<ApiResponseData<PositionFavorite>>({
      url: "/internship/position/favorite",
      method: "post",
      data: { positionId }
    })
  } else {
    return request<ApiResponseData<void>>({
      url: `/internship/position/favorite/${positionId}`,
      method: "delete"
    })
  }
}
